package com.hmall.api.config;

import com.hmall.api.client.fallback.ItemClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2024-10-10 14:55:34
 */
@Configuration
@Slf4j
public class DefaultFeignConfig {

    @Bean
    public Logger.Level level(){
        return Logger.Level.NONE;
    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> {
            Long user = UserContext.getUser();
            log.info("在发起OpenFeign调用之前，从ThreadLocal上获取用户Id：{}", user);
            if (user == null) {
                return;
            }
             template.header("user-info", user.toString());
      };
    }

    @Bean
    public ItemClientFallback itemClientFallback() {
        return new ItemClientFallback();
    }
}
